$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};
var _modalOptions;
var _modalminWidth;
var _modalminHeight;

function setModal(url, optionString, minWidth, minHeight) {
	//store settings for use on reload;
	_modalOptions = optionString;
	_modalminHeight = minHeight;
	_modalminWidth = minWidth;

	var iframe = $('#modaldialogiframe');
	var myDialog = $('#modaldialog');
	iframe.hide();
	//do a reset (in case values changed from last modal load)
	iframe.contents().remove();
	myDialog.parent().css("opacity", 1);
	if (!isNaN(optionString.width)) {
		minWidth = optionString.width;
	}
	if (!isNaN(optionString.height)) {
		minHeight = optionString.height;
	}
	iframe.unbind();
	myDialog.unbind();
	optionString.width = 100;
	optionString.height = 100;
	//show a loading screen
	myDialog.addClass("a65chromeLoading").dialog('option', { title: "Loading", width: 100, height: 100, position: 'center' });
	iframe.width(100).height(100);
	//reset the frame and start the load
	iframe.attr("src", "").attr("src", url);
	//stop animations on close
	myDialog.bind("dialogbeforeclose", function(event, ui) {
		$('#modaldialog').stop();
		$('#modaldialog').parent(".ui-dialog").stop();
		$('#modaldialogiframe').stop();
		$('#modaldialogiframe').contents().find('#wrapper_content').unbind("resize");
		$('#modaldialog').addClass("a65chromeLoading");
	});
	myDialog.dialog('open');
	myDialog.parent().prev('.ui-widget-overlay').css("opacity", "0.55");
	iframe.load(function () {
		var iframe_content = $('#modaldialogiframe').contents().find('#wrapper_content');
		iframe_content.unbind("resize");
		animateModal(optionString, minWidth, minHeight, true);
		//unbind so that if the iframe posts in itself, it won't reanimate
		iframe.unbind('load');
		iframe_content.unbind("resize");
		iframe.bind('load', function () {
			$('#modaldialogiframe').contents().find('#wrapper_content').unbind("resize");
			animateModal(_modalOptions, _modalminWidth, _modalminHeight, false);
		});
        // try/catch for IE, which may have invalidated the contents of the iframe

        try {

            iframe_content.resize(function () {
                resizeModal();
            });
        } catch (ie) {}
		
	});
};
function animateModal(optionString, minWidth, minHeight, firstLoad) {
	var iframe = $('#modaldialogiframe');
	var myDialog = $('#modaldialog');
		if (myDialog.dialog('isOpen')) {
			//upon load
			var dialogParent = myDialog.parent().parent().parent().parent(".ui-dialog");
			myDialog.removeClass("a65chromeLoading");
			myDialog.addClass("a65setOverflow");
			if (firstLoad) iframe.show().css("visibility", "hidden");

			//Execute any initializations for modals (photo handler)
			if (typeof document.getElementById('modaldialogiframe').contentWindow.initPhotoHandler == "function") { document.getElementById('modaldialogiframe').contentWindow.initPhotoHandler(); }

			var iframe_content = iframe.contents().find('#wrapper_content');
			iframe_content.unbind('resize');

			//get iframe width/height, and parent width/height
			var childW = iframe_content.outerWidth();
			var childH = iframe.contents().find('#header').outerHeight() + iframe.contents().find('#modalcontent').outerHeight() + iframe.contents().find('#footer').outerHeight();
			var parentW = $(window).width(), parentH = $(window).height();

			//add padding
			childW = childW;
			childH = childH;
			//check for mins
			childW = (childW < minWidth) ? minWidth : childW;
			childH = (childH < minHeight) ? minHeight : childH;
			var calcW = (childW < parentW - 200) ? childW : parentW - 200;
			var calcH = (childH < parentH - 200) ? childH : childH;
			//set options
			myDialog.dialog('option', 'title', optionString.title);

			if (firstLoad) iframe.css("visibility", "visible");
			if (firstLoad) iframe.hide();
			//sets iframe height, width
			var newFrameW, newFrameH, xWindow;
			newFrameW = (childW = calcW) ? "100%" : childW;
			newFrameH = childH;
			iframe.height(newFrameH);
			iframe.width(newFrameW);
			dialogParent.animate({
				top: (childH < parentH - 200) ? ((parentH - calcH) / 2) + $(window).scrollTop() - 50 : 10 + $(window).scrollTop(),
				left: ((parentW - calcW) / 2) + $(window).scrollLeft() - 50,
				width: calcW
			}, 'fast', function () {
				myDialog.animate({ height: calcH, width: calcW
				}, 'fast', function () {
					iframe.fadeIn('fast', function () {
						//remove load
						myDialog.removeClass("a65setOverflow");
						iframe.contents().find('html').css("overflow", "hidden");
						iframe_content.addClass("a65modalinterior");
					});
				});
			});
		};
}
function resizeModal() {
	var iframe = $('#modaldialogiframe');
	var myDialog = $('#modaldialog');
	var elem = iframe.contents().find('.a65modalinterior'); ;
	var dialogParent = myDialog.parents(".ui-dialog");
	if (iframe.is(":visible") && myDialog.is(":visible") || myDialog.dialog('isOpen')) {
		var heightDiff, widthDiff;
		var thisOffset = dialogParent.offset();
		if (Math.abs(iframe.width() - elem.outerWidth()) > 0 && elem.outerWidth() > 0) {
			widthDiff = elem.outerWidth() - iframe.width();
			iframe.width(elem.outerWidth());
			myDialog.animate({ width: myDialog.width() + widthDiff });
			dialogParent.animate({
				width: dialogParent.width() + widthDiff,
				left: thisOffset.left - (widthDiff / 2)
			}, 'fast');
		}
		if (Math.abs(iframe.height() - elem.outerHeight()) > 0 && elem.outerHeight() > 0) {
			heightDiff = elem.outerHeight() - iframe.height();
			iframe.height(elem.outerHeight());
			dialogParent.animate({
				top: thisOffset.top - (heightDiff / 2)
			}, 'fast', function () {
				myDialog.animate({ height: elem.outerHeight(true) }, 'fast');
			});
		}
	} else {
		myDialog.dialog('close');
	};
}
function adjustModal(contentHeight, minHeight, parentHeight) {
	var calcH = (contentHeight < minHeight) ? minHeight : contentHeight;

	var iframe = $('#modaldialogiframe');
	var myDialog = $('#modaldialog');

	iframe.height(contentHeight);
	myDialog.animate({ height: calcH });
}
function showAdvancedTab(id) {
	$("#VolusionHelpToolTip").css("visibility", "hidden");
    $(".AdvancedTab").removeClass("Selected");
    $("#AdvancedTab_" + id.toString()).addClass("Selected");
    $(".AdvancedTab a").blur();
    $(".AdvancedTabGroup:not(#AdvancedTabGroup_" + id.toString() + ")").hide();    
    $("#AdvancedTabGroup_" + id.toString()).show();    
    $("#NOSAVE___AdvancedTabSelected").val(id);
    return false;
}
function getExpandedSections() {
    var str_cookie = GetCookie("ExpandedSections");
    var arr_cookie;
    var len_cookie;
    var i = 0;
    var arr_key_value = "";
    var str_key = "";
    var str_value = "";
    var returnstring = "";
    
    if (vTrim(str_cookie) == "") {
        return returnstring;
    } else {
        str_cookie = str_cookie + "&";
        arr_cookie = str_cookie.split("&");
        len_cookie = arr_cookie.length;

        for (i = 0; i < len_cookie; i++) {
            if (vTrim(arr_cookie[i]) != "") {
                arr_key_value = arr_cookie[i].split("=");
                str_key = arr_key_value[0];
                str_value = arr_key_value[1];

                if (vTrim(str_value) == "Y") {
                    returnstring = returnstring + "#" + str_key + ", ";
                }
            }
        }
        if (returnstring.length > 0) {
            returnstring = returnstring.slice(0, returnstring.length - 2);
        }

        return returnstring;
    }
   }
   function init65chrome(exceptionString) {
	//default 'reset' position
   	$(".a65chromepanel:not(.a65noncollapse) > .a65chromecontent").not(exceptionString).hide();
	//expand first panel
	
	$("div[id$='detail_page_section_1']").each(function() {
		$(this).slideDown('fast');
		$(this).prev().children(".arrow_link").addClass("active");
		$(this).prev().children(".a65chromeheadertext").toggleClass("expandedtext");
	});
	//expand panels that were expanded previously, except first panel
	var ExpandedSections = "";
	ExpandedSections = ExpandedSections + getExpandedSections();
	if (ExpandedSections.length > 0) {
		$(getExpandedSections()).not(exceptionString).each(function() {
			$(this).slideToggle('fast');
			$(this).prev().children(".arrow_link").toggleClass("active");
			$(this).prev().children(".a65chromeheadertext").toggleClass("expandedtext");
		});
	};
	//expand hidden panels that were set to noncollapsible
	$(".a65chromepanel.a65noncollapse > .a65chromecontent").show();
	

	//add expanding functionality
	$(".a65chromepanel:not(.a65noncollapse) > .a65chromeheader").not(exceptionString).click(function () {
		$(".arrow_link", this).toggleClass("active");
		$(".a65chromeheadertext", this).toggleClass("expandedtext");
		$(this).next(".a65chromecontent").slideToggle('fast');
		storeToggle($(".arrow_link", this).hasClass("active"), $(this).next(".a65chromecontent").attr("id"));

	});
	//wrap headers for curved images
	$(".a65chromeheader").each(function() {
		$(this).children().wrapAll("<div class='a65chromeheader_left'>");
		$(this).children().wrapAll("<div class='a65chromeheader_right'>");
	});
	//wrap headers for curved images
	$(".a65chromefooter").each(function () {
		$(this).children().wrapAll("<div class='a65chromefooter_left'>");
		$(this).children().wrapAll("<div class='a65chromefooter_right'>");
	});
	//add styling for advanced panels
	$(".a65chromecontent .AdvancedTabPanel").parents(".a65chromecontent").addClass("a65advancedpanel");
	$(".a65chromecontent .AdvancedTabPanel").parents(".a65chromecontent").children().wrapAll("<div class='a65settingsbox' />");

}
function storeToggle(isExpanded, thisID) {
	isExpanded = (isExpanded == true) ? "Y" : "";
	SetCookie("ExpandedSections", isExpanded, 1 * c_years, thisID);
}
function collapseAll() {
	$(".a65chromepanel:not(.a65noncollapse) > .a65chromecontent").each(function() {
		if ($(this).attr("style") != "display: none;") { $(this).slideToggle('fast'); }
		$(".arrow_link.active", $(this).prev(".a65chromeheader")).toggleClass("active");
		storeToggle($(".arrow_link", $(this).prev(".a65chromeheader")).hasClass("active"), $(this).attr("id"));
	});
	//for existing slidertables
	//for existing slidertables
	$("[onClick*='sliderToggle']").each(function() {
		var onCA = $(this).attr("onClick").split(",");
		$(this).removeClass("open").addClass("closed");
		var toActOn = onCA[1].replace("'", "").replace("');", "").replace(" ", "");
		$("#" + toActOn).slideUp('fast');
		storeToggle(false, toActOn);
	});
}
function expandAll() {
	$(".a65chromepanel:not(.a65noncollapse) > .a65chromecontent").each(function() {
	if ($(this).attr("style") != "display: block;") { $(this).slideToggle('fast'); }
		$(".arrow_link:not(.active)", $(this).prev(".a65chromeheader")).toggleClass("active");
		storeToggle($(".arrow_link", $(this).prev(".a65chromeheader")).hasClass("active"), $(this).attr("id"));
	});

	//for existing slidertables
	$("[onClick*='sliderToggle']").each(function() {
		var onCA = $(this).attr("onClick").split(",");
		$(this).removeClass("closed").addClass("open");
		var toActOn = onCA[1].replace("'", "").replace("');", "").replace(" ", "");
		$("#" + toActOn).slideDown('fast');
		storeToggle(true, toActOn);
	});

}
function dynamicHeight() {
}
function iframeOnload(obj) {
	//$("#loadingdialog").dialog('close');
}
function sliderToggle(orig, toActOn) {
	var expanded;
	if ( $(orig).hasClass("open") ) {
		$(orig).removeClass("open").addClass("closed");
		$("#" + toActOn).slideUp('fast');
		expanded = false;
	} else {
		$(orig).removeClass("closed").addClass("open");
		$("#" + toActOn).slideDown('fast');
		expanded = true;
	}
	storeToggle(expanded, toActOn);

}